package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.PillEducationBo;
import com.hospital.workstation.domain.vo.PillEducationVo;
import com.hospital.workstation.domain.PillEducation;
import com.hospital.workstation.mapper.PillEducationMapper;
import com.hospital.workstation.service.IPillEducationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 患者用药教育Service业务层处理
 *
 * @author whb
 * @date 2023-03-10
 */
@RequiredArgsConstructor
@Service
public class PillEducationServiceImpl implements IPillEducationService {

    private final PillEducationMapper baseMapper;

    /**
     * 查询患者用药教育
     */
    @Override
    public PillEducationVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询患者用药教育列表
     */
    @Override
    public TableDataInfo<PillEducationVo> queryPageList(PillEducationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PillEducation> lqw = buildQueryWrapper(bo);
        Page<PillEducationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询患者用药教育列表
     */
    @Override
    public List<PillEducationVo> queryList(PillEducationBo bo) {
        LambdaQueryWrapper<PillEducation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PillEducation> buildQueryWrapper(PillEducationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PillEducation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, PillEducation::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(StringUtils.isNotBlank(bo.getEducationContent()), PillEducation::getEducationContent, bo.getEducationContent());
        return lqw;
    }

    /**
     * 新增患者用药教育
     */
    @Override
    public Boolean insertByBo(PillEducationBo bo) {
        PillEducation add = BeanUtil.toBean(bo, PillEducation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改患者用药教育
     */
    @Override
    public Boolean updateByBo(PillEducationBo bo) {
        PillEducation update = BeanUtil.toBean(bo, PillEducation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PillEducation entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除患者用药教育
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据病历号获取用药教育
     *
     * @param medicalRecordNo 病历号
     * @return
     */
    @Override
    public PillEducationVo queryByMedicalRecordNo(Integer medicalRecordNo) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<PillEducation>()
            .eq(PillEducation::getMedicalRecordNo, medicalRecordNo));
    }

    /**
     * 保存用药教育信息
     *
     * @param bo 用药教育业务对象
     * @return
     */
    @Override
    public Boolean save(PillEducationBo bo) {
        if (bo.getId() != null) { // 更新
            return updateByBo(bo);
        } else { // 新增
            return insertByBo(bo);
        }
    }
}
